#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int mod=998244353,inf=0x3f3f3f3f3f3f3f3f;
const int N=2e5+10,M=2e5+10;
mt19937 rnd(time(0));
int f[40][2][2];
const int W=30;
int solve(int N,int M)
{
	memset(f,0,sizeof(f));
	f[W][0][0]=1;
	for ( int i = W ; i > 0 ; i-- )
	for ( int fn = 0 ; fn <= 1 ; fn++ )
	for ( int fm = 0 ; fm <= 1 ; fm++ )
	{
		for ( int cn = 0 ; cn <= 1 ; cn++ )
		for ( int cm = 0 ; cm <= cn ; cm++ )
		{
			if(fn==0&&((N>>i-1)&1)==0&&cn)continue;
			if(fm==0&&((M>>i-1)&1)==0&&cm)continue;
			int ni=i-1,nfn=fn|((N>>i-1)&(cn^1)),nfm=fm|((M>>i-1)&(cm^1));
			f[ni][nfn][nfm]+=f[i][fn][fm];
		}
	}
	return f[0][0][0]+f[0][0][1]+f[0][1][0]+f[0][1][1];
}
int s2(int l,int r)
{
	int U=(1ll<<30)-1,ans=0;;
	for ( int i = l ; i <= r ; i++ )
		for ( int j = l ; j <= r ; j++ )
		{
			if((i^U)&j)ans+=0;
			else ans++;
		}
	return ans;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	freopen("comb.in","r",stdin);
	freopen("comb.out","w",stdout);
//	cerr << solve(1,2) << endl;
	int q;cin >> q;
	while(q--)
	{
		int l,r;cin >> l >> r;
		int ans=solve(r,r)-solve(l-1,r)-solve(r,l-1)+solve(l-1,l-1);
		cout << ans << endl;
//		cerr << "correct:" << s2(l,r) << endl;
	}
	return 0;
}
/*
1
1 8
*/

